import re

from django.shortcuts import render,redirect

# Create your views here.

from db import models
import requests
import time
import subprocess
def cdnrefresh(request):
    if request.method == "GET":
        apk_url = request.GET.get("apk_url")
        cdn_info = models.cdnconfig.objects.filter(cdn_enable=1).values()[0]
        file_cdn_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        print("++++++++++cdn type+++++++++++++")
        print(cdn_info["cdn_name"])
        if cdn_info["cdn_name"] == "baishan":
            api_url = cdn_info["api_url"]
            headers = {"Content-Type": "application/json"}
            data = {
                "urls":[apk_url,],
                "type":"url",
            }
            result = requests.post(api_url,headers=headers,json=data)
            result_tmp = eval(result.text)
            if result_tmp["code"] == 0:
                models.fileinfo.objects.filter(file_url=apk_url).update(file_cdn_status=result_tmp["code"],file_cdn_time=file_cdn_time)
            else:
                models.fileinfo.objects.filter(file_url=apk_url).update(file_cdn_status=result_tmp["code"])

        elif cdn_info["cdn_name"] == "aliyun":
            access_key_id = cdn_info["access_key_id"]
            access_key_secret = cdn_info["access_key_secret"]
            from public import aliyun
            obj = aliyun.aliyunsdk(access_key_id,access_key_secret)
            result = obj.RefreshObjectCachesRequest(apk_url)
            result_dict = eval(result)
            print("++++++++++++aliyun cdn result++++++++++++")
            print(result_dict)
            if result_dict["RefreshTaskId"]:
                models.fileinfo.objects.filter(file_url=apk_url).update(file_cdn_status="0",
                                                                    file_cdn_time=file_cdn_time)
            else:
                models.fileinfo.objects.filter(file_url=apk_url).update(file_cdn_status="1")
        elif cdn_info["cdn_name"] == "tencent":
            access_key_id = cdn_info["access_key_id"]
            access_key_secret = cdn_info["access_key_secret"]
            from public import tencent
            obj = tencent.tencentsdk(access_key_id,access_key_secret)
            result = obj.PurgeUrlsCache(apk_url)
            result_dict = eval(result)
            if result_dict["TaskId"]:
                models.fileinfo.objects.filter(file_url=apk_url).update(file_cdn_status="0",
                                                                    file_cdn_time=file_cdn_time)
            else:
                models.fileinfo.objects.filter(file_url=apk_url).update(file_cdn_status="1")

        return redirect("/user/myfile/")
def baishan(request):
    user_name = request.session.get("user_name")
    info = {"user_name": user_name}
    if request.method == "GET":
        cdn_status = models.cdnconfig.objects.filter(cdn_name="baishan").count()
        if cdn_status == 0:
            info["api_url"] = ""
        else:
            cdn_api_url = models.cdnconfig.objects.filter(cdn_name="baishan").values("api_url")[0]["api_url"]
            info["api_url"] = cdn_api_url
        return render(request,"other/baishan.html",info)
    else:
        api_url = request.POST.get("api_url")
        cdn_name = "baishan"
        total_status = models.cdnconfig.objects.filter(cdn_name="baishan").count()
        if total_status == 0:
            models.cdnconfig.objects.create(cdn_name=cdn_name,api_url=api_url)
        else:
            models.cdnconfig.objects.filter(cdn_name=cdn_name).update(api_url=api_url)
        return redirect("/cdn/baishan/")
def aliyun(request):
    user_name = request.session.get("user_name")
    info = {"user_name": user_name}
    if request.method == "GET":
        cdn_status = models.cdnconfig.objects.filter(cdn_name="aliyun").count()
        if cdn_status == 0:
            cdn_info = {"access_key_id":0,"access_key_secret":0}
        else:
            cdn_info = models.cdnconfig.objects.filter(cdn_name="aliyun").values()[0]
        info["cdn_info"] = cdn_info
        return render(request, "other/aliyun.html", info)
    else:
        access_key_id = request.POST.get("access_key_id")
        access_key_secret = request.POST.get("access_key_secret")
        cdn_name = "aliyun"
        total_status = models.cdnconfig.objects.filter(cdn_name="aliyun").count()
        if total_status == 0:
            models.cdnconfig.objects.create(cdn_name=cdn_name, access_key_id=access_key_id,access_key_secret=access_key_secret)
        else:
            models.cdnconfig.objects.filter(cdn_name=cdn_name).update(access_key_id=access_key_id,access_key_secret=access_key_secret)
        return redirect("/cdn/aliyun/")

def tencent(request):
    user_name = request.session.get("user_name")
    info = {"user_name": user_name}
    if request.method == "GET":
        cdn_status = models.cdnconfig.objects.filter(cdn_name="tencent").count()
        if cdn_status == 0:
            cdninfo = {"access_key_id":0,"access_key_secret":0}
        else:
            cdninfo = models.cdnconfig.objects.filter(cdn_name="tencent").values()[0]
        info["cdn_info"] = cdninfo
        return render(request,"other/tencent.html",info)
    else:
        access_key_id = request.POST.get("access_key_id")
        access_key_secret = request.POST.get("access_key_secret")
        tencent_status = models.cdnconfig.objects.filter(cdn_name="tencent").count()
        if tencent_status == 0:
            models.cdnconfig.objects.create(access_key_id=access_key_id, access_key_secret=access_key_secret,
                                            cdn_name="tencent",
                                            )
        else:
            models.cdnconfig.objects.filter(cdn_name="tencent").update(access_key_id=access_key_id,
                                                                       access_key_secret=access_key_secret,cdn_name="tencent",)
        return redirect("/cdn/tencent/")
